package gate

func (gate *Gate) login(source uint, args []interface{}) (ret interface{}, err error) {
	username := args[0].(string)
	secret := args[1].([]byte)
	uid, subid, _, err := decodeUserName(username)
	if err != nil {
		return
	}
	u, ok := gate.onlineUsers[uid]
	if ok {
		return
	}
	u = new(UserData)
	u.secret = secret
	u.version = 0
	u.index = 0
	u.uid = uid
	u.subid = subid
	gate.onlineUsers[uid] = u
	return
}

func (gate *Gate) logout(source uint, args []interface{}) (ret interface{}, err error) {
	username := args[0].(string)
	uid, subid, _, err := decodeUserName(username)
	if err != nil {
		return
	}
	u, ok := gate.onlineUsers[uid]
	if !ok {
		return
	}
	if u.subid != subid {
		return
	}
	delete(gate.onlineUsers, uid)
	if u.fd != 0 {
		u.fd = 0
		gate.CloseAgent(u.fd)
	}
	return
}

func (gate *Gate) kick(source uint, args []interface{}) (ret interface{}, err error) {
	uid := args[0].(uint64)
	subid := args[1].(int)
	u, ok := gate.onlineUsers[uid]
	if !ok {
		return
	}
	if u.subid != subid {
		return
	}
	delete(gate.onlineUsers, uid)
	if u.fd != 0 {
		u.fd = 0
		gate.CloseAgent(u.fd)
	}
	return
}
